import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:game2048/digital_evolution/audio_player_manager.dart';
import 'package:game2048/digital_evolution/game_checkpoint.dart';
import 'package:game2048/digital_evolution/game_page.dart';
import 'package:game2048/main.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1), () {
      AudioManager().playBackgroundMusic("assets/sounds/bg_audio.mp3");
    });
  }

  @override
  void dispose() {
    AudioManager().stopBackgroundMusic(); // 确保在组件销毁时停止音乐
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      builder: EasyLoading.init(),
      title: 'digitalEvolution'.tr(),
      theme: ThemeData(primarySwatch: Colors.blue),
      home: WelcomePage(),
    );
  }
}

class WelcomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/bg.png'), // 使用本地背景图片
            fit: BoxFit.cover,
          ),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 使用本地图片作为logo
              Image.asset(
                'assets/logo.png', // 2048数字进化的本地图片
                width: 200,
                height: 100,
              ),

              // 开始游戏按钮
              SizedBox(height: 200),
              GlobalMethods.buildStartButton(() {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => LevelSelectionPage()),
                );
              }, "startGame".tr()),
            ],
          ),
        ),
      ),
    );
  }
}

class GlobalMethods {
  // 构建开始按钮
  static Widget buildStartButton(
    Function buttonPressCallback,
    String buttonText,
  ) {
    return Container(
      width: 300,
      height: 60,
      decoration: BoxDecoration(
        color: Colors.yellow,
        border: Border.all(color: Colors.brown, width: 4),
        borderRadius: BorderRadius.circular(30),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.2),
            spreadRadius: 2,
            blurRadius: 10,
            offset: Offset(0, 3),
          ),
        ],
      ),
      child: ElevatedButton(
        onPressed: () {
          buttonPressCallback();
        },
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.yellow,
          foregroundColor: Colors.brown,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30),
          ),
          padding: EdgeInsets.all(4),
        ),
        child: Text(
          buttonText,
          style: TextStyle(
            fontSize: 26,
            fontWeight: FontWeight.bold,
            color: Colors.brown,
          ),
        ),
      ),
    );
  }
}
